<template>
	<div class="container">
		<div class="navList">
			<router-link to="/userList">
				<span class="spanUser">用户管理</span>
				<span class="spanArrow">></span>
			</router-link>
			<span class="spanEdit">编辑用户</span>
		</div>

		<section class="editUserContent">
			<div class="editUserForm">
				<el-form ref="form" :model="form" label-width="80px">
					<el-form-item label="账号名称" prop="accountName" :rules="[{required: true, message: '请输入用户名称', trigger: 'blur'}]">
						<el-input v-model="form.accountName" disabled="disabled"></el-input>
					</el-form-item>
					<el-form-item label="真实姓名" prop="userName" :rules="[{required: true, message: '请输入真实姓名', trigger: 'blur'}]">
						<el-input v-model="form.userName"></el-input>
					</el-form-item>
					<el-form-item label="性别" prop="sex">
						<el-select v-model="form.sex" placeholder="请选择性别">
							<el-option label="男" value="M"></el-option>
							<el-option label="女" value="F"></el-option>
							<el-option label="其他" value="O"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="生日" prop="birthday">
						<el-date-picker type="date" placeholder="请选择日期" v-model="form.birthday" style="width: 100%"></el-date-picker>
					</el-form-item>
					<el-form-item label="电话号码" prop="telephone" :rules="[{required: false, pattern: /^1[34578]\d{9}$|^([0-9]{3,4}-)?[0-9]{7,8}$/, message: '请输入正确格式', trigger: 'change'}]">
						<el-input v-model="form.telephone"></el-input>
					</el-form-item>
					<el-form-item label="初始密码" prop="accountPwd">
						<el-input v-model="form.accountPwd" :disabled="true"></el-input>
					</el-form-item>
					<el-form-item label="所属机构" prop="institutionName" :rules="[{required: true, message: '请选择所属机构', trigger: 'change'}]">
						<el-select v-model="form.institutionName" placeholder="请选择所属机构">
							<el-option label="机构一" value="1"></el-option>
							<el-option label="机构二" value="2"></el-option>
						</el-select>
					</el-form-item>
					<!-- <el-form-item label="用户角色" prop="rule" :rules="[{type: 'array', required: true, message: '请至少选择一个角色', trigger: 'change'}]">
						<el-checkbox-group v-model="form.rule">
							<el-checkbox label="客服" name="rule"></el-checkbox>
							<el-checkbox label="技师" name="rule"></el-checkbox>
							<el-checkbox label="分片医生" name="rule"></el-checkbox>
							<el-checkbox label="初级报告医生" name="rule"></el-checkbox>
							<el-checkbox label="初级审核医生" name="rule"></el-checkbox>
							<el-checkbox label="高级报告医生" name="rule"></el-checkbox>
							<el-checkbox label="高级审核医生" name="rule"></el-checkbox>
						</el-checkbox-group>
					</el-form-item> -->
					<el-form-item label="职位信息" prop="position" :rules="[{required: true, message: '请输入职位信息', trigger: 'blur'}]">
						<el-input v-model="form.position"></el-input>
					</el-form-item>
					<el-form-item>
						<el-button size="small" type="primary" @click.native.prevent="saveEdit">保存</el-button>
						<el-button size="small" @click.native.preven="goBack">返回</el-button>
					</el-form-item>
				</el-form>
			</div>
		</section>
	</div>
</template>

<script>
import { searchUser, updateUser } from "../../api/api";
import util from "../../common/util";
export default {
  data() {
    return {
      form: {
        accountName: "",
        userName: "",
        sex: "",
        birthday: "",
        telephone: "",
        accountPwd: "123456",
        institutionName: "",
        //rule: [],
        job: ""
      }
    };
  },
  methods: {
    getId() {
      //获取当前id修改信息
      var _this = this;
      let paramData = {
        id: this.$route.params.id
      };
      searchUser(paramData).then(function(res) {
        if (res.data.status === "200") {
          _this.form = res.data.data[0];
          _this.form.accountPwd = "123456";
          if (_this.form.sex === "M") {
            _this.form.sex = "男";
          } else if (_this.form.sex === "F") {
            _this.form.sex = "女";
          } else if (_this.form.sex === "O") {
            _this.form.sex = "其他";
          }
        }
      });
    },
    saveEdit() {
      //保存修改信息
      var _this = this;
      this.$refs.form.validate(function(valid) {
        if (valid) {
          //保存处理
          if (_this.form.sex === "男") {
            _this.form.sex = "M";
          } else if (_this.form.sex === "女") {
            _this.form.sex = "F";
          } else if (_this.form.sex === "其他") {
            _this.form.sex = "O";
          }
          let para = Object.assign({}, _this.form);
          para.birthday =
            !para.birthday || para.birthday == ""
              ? ""
              : util.formatDate.format(new Date(para.birthday), "yyyy-MM-dd");
          console.log("111", para);
          updateUser(para).then(function(res) {
            if (res.data.status === "200") {
              _this.$router.push({ path: "/userList" });
            }
          });
        } else {
          return false;
        }
      });
    },
    goBack() {
      this.$router.push({ path: "/userList" });
    }
  },
  mounted() {
    this.getId();
  }
};
</script>

<style lang="scss" scoped>
.navList {
  height: 55px;
  text-align: left;
  border-bottom: 4px solid #cdd1da;
  line-height: 55px;
  padding-left: 20px;
  .spanUser {
    color: #333;
    font-size: 22px;
  }
  .spanArrow {
    color: #333;
    font-size: 16px;
    padding-left: 5px;
  }
  .spanEdit {
    font-size: 15px;
  }
  a {
    text-decoration: none;
  }
  .spanUser:hover {
    color: #3c8dbc;
  }
}

.editUserContent {
  width: 100%;
  height: 650px;
  background: #fff;
  padding-top: 10px;
}

.editUserForm {
  width: 540px;
  padding: 5px 10px 10px;
  margin-left: 190px;
  border: solid 3px #eaeaea;
  border-radius: 5px;
  box-shadow: 0px 0px 10px #dcdcdc;
}

.editUserForm .el-select {
  display: block;
}
</style>